Language Reference / Predefined Exception
Exception
Exception is the base class for all Exceptions in PHP 5, and the base class for all user exceptions in PHP 7.
Class synopsis
Exception {
/* Properties */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Methods */
public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] )
final public string getMessage ( void )
final public Throwable getPrevious ( void )
final public mixed getCode ( void )
final public string getFile ( void )
final public int getLine ( void )
final public array getTrace ( void )
final public string getTraceAsString ( void )
public string __toString ( void )
final private void __clone ( void )
}
Properties
- message: The exception message
- code: The exception code
- file: The filename where the exception was created
- line: The line where the exception was created
ErrorException
An Error Exception.
Class synopsis
ErrorException extends Exception {
/* Properties */
protected int $severity ;
/* Inherited properties */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Methods */
public __construct ([ string $message = "" [, int $code = 0 [, int $severity = E_ERROR [, string $filename = __FILE__ [, int $lineno = __LINE__ [, Exception $previous = NULL ]]]]]] )
final public int getSeverity ( void )
/* Inherited methods */
final public string Exception::getMessage ( void )
final public Throwable Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}
Properties
severity: The severity(嚴重) of the exception
Examples: Use set_error_handler() to change error messages into ErrorException.
<?php
function exception_error_handler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
// This error code is not included in error_reporting
return;
}
throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");
/* Trigger exception */
strpos();
/*
Fatal error: Uncaught exception 'ErrorException' with message 'strpos() expects at least 2 parameters, 0 given' in /home/bjori/tmp/ex.php:12
Stack trace:
#0 [internal function]: exception_error_handler(2, 'strpos() expect...', '/home/bjori/php...', 12, Array)
#1 /home/bjori/php/cleandocs/test.php(12): strpos()
#2 {main}
thrown in /home/bjori/tmp/ex.php on line 12
*/
?>
Error
Error is the base class for all internal PHP errors.
Class synopsis
Error implements Throwable {
/* Properties */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Methods */
public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] )
final public string getMessage ( void )
final public Throwable getPrevious ( void )
final public mixed getCode ( void )
final public string getFile ( void )
final public int getLine ( void )
final public array getTrace ( void )
final public string getTraceAsString ( void )
public string __toString ( void )
final private void __clone ( void )
}
Properties
- message: The error message
- code: The error code
- file: The filename where the error happened
- line: The line where the error happened
ArgumentCountError
ArgumentCountError is thrown when too few arguments are passed to a user-defined function or method.
ArithmeticError
ArithmeticError(算術) is thrown when an error occurs while performing(執行) mathematical(算數) operations. In PHP 7.0, these errors include attempting to perform a bitshift(位移) by a negative amount, and any call to intdiv() that would result in a value outside the possible bounds of(邊界) an integer.
AssertionError
AssertionError is thrown when an assertion made via assert() fails.
DivisionByZeroError
DivisionByZeroError is thrown when an attempt is made to divide a number by zero
ParseError
ParseError is thrown when an error occurs while parsing PHP code, such as when eval() is called.
TypeError
There are three scenarios(場景) where a TypeError may be thrown
- the argument(參數) type being passed to a function does not match its corresponding declared parameter type
- a value being returned from a function does not match the declared function return type
- invalid number of arguments are passed to a built-in PHP function (strict mode only).